package com.zblx.lcd;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.easysocket.EasySocket;
import com.easysocket.config.EasySocketOptions;
import com.easysocket.entity.OriginReadData;
import com.easysocket.entity.SocketAddress;
import com.easysocket.interfaces.conn.ISocketActionListener;
import com.easysocket.interfaces.conn.SocketActionListener;
import com.easysocket.utils.LogUtil;
import com.zblx.lcd.util.SystemTTS;
import com.zblx.lcd.view.MarqueeTextView;
import com.zblx.lcd.websocket.BussinessWebSocketManager;


import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends BaseActivity {

    public BussinessWebSocketManager systemWebSocket;

    @BindView(R.id.edt1)
    TextView edt1;

    @BindView(R.id.pmd1)
    MarqueeTextView pmd1;
    @BindView(R.id.pmd2)
    MarqueeTextView pmd2;
    @BindView(R.id.pmd3)
    MarqueeTextView pmd3;
    @BindView(R.id.pmd4)
    MarqueeTextView pmd4;

    SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        pmd1.setText("欢迎使用帮驻智停停车收费系统 现在时间是："+sdf1.format(new Date()));
        pmd2.setText("欢迎使用帮驻智停停车收费系统 现在时间是："+sdf1.format(new Date()));
        pmd3.setText("欢迎使用帮驻智停停车收费系统 现在时间是："+sdf1.format(new Date()));
        pmd4.setText("欢迎使用帮驻智停停车收费系统 现在时间是："+sdf1.format(new Date()));


        pmd1.Start();
        pmd2.Start();
        pmd3.Start();
        pmd4.Start();


        initEasySocket();

    }

    /**
     * Socket
     */
    private void initEasySocket() {

        //socket配置
        EasySocketOptions options = new EasySocketOptions.Builder()
                .setSocketAddress(new SocketAddress("10.192.84.41",19730)) //主机地址
                .build();

        //初始化EasySocket
        EasySocket.getInstance()
                .options(options) //项目配置
                .createConnection();


        /**
         * socket行为监听
         */
         ISocketActionListener socketActionListener = new SocketActionListener() {
            /**
             * socket连接成功
             * @param socketAddress
             */
            @Override
            public void onSocketConnSuccess(SocketAddress socketAddress) {
                super.onSocketConnSuccess(socketAddress);
                LogUtil.d("连接成功");
            }

            /**
             * socket连接失败
             * @param socketAddress
             * @param isNeedReconnect 是否需要重连
             */
            @Override
            public void onSocketConnFail(SocketAddress socketAddress, Boolean isNeedReconnect) {
                super.onSocketConnFail(socketAddress, isNeedReconnect);
            }

            /**
             * socket断开连接
             * @param socketAddress
             * @param isNeedReconnect 是否需要重连
             */
            @Override
            public void onSocketDisconnect(SocketAddress socketAddress, Boolean isNeedReconnect) {
                super.onSocketDisconnect(socketAddress, isNeedReconnect);
                LogUtil.d("socket断开连接，是否需要重连："+isNeedReconnect);
            }

            /**
             * socket接收的数据
             * @param socketAddress
             * @param originReadData
             */
            @Override
            public void onSocketResponse(SocketAddress socketAddress, OriginReadData originReadData) {
                super.onSocketResponse(socketAddress, originReadData);
                LogUtil.d("socket监听器收到数据=" + originReadData.getBodyString());
            }
        };

        // 监听socket行为
        EasySocket.getInstance().subscribeSocketAction(socketActionListener);



    }


    MediaPlayer mMediaPlayer;
    @OnClick({R.id.but1,R.id.but2,R.id.but3,R.id.but4})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.but1:
                selectd();
                break;
            case R.id.but2:
                SystemTTS.getInstance(this).playText("这是一段用于测试语音的操作");
                //speechText("这是一段用于测试语音的操作");
                break;
            case R.id.but3:
                mMediaPlayer= MediaPlayer.create(this, R.raw.open_success);
                mMediaPlayer.start();
                break;
            case R.id.but4:
                mMediaPlayer= MediaPlayer.create(this, R.raw.lock_success);
                mMediaPlayer.start();
                break;
        }
    }


    private void selectd(){
        try{
            Intent intentw = new Intent(Intent.ACTION_MAIN);
            intentw.addCategory(Intent.CATEGORY_HOME);
            intentw.setClassName("android","com.android.internal.app.ResolverActivity");
            startActivity(intentw);
        }catch (Exception e){}
    }
}
